package me.caofeng.service;

import com.sun.org.apache.regexp.internal.RE;
import me.caofeng.dao.CartMapper;
import me.caofeng.dao.ProductMapper;
import me.caofeng.enums.CheckStatus;
import me.caofeng.enums.ResponseCode;
import me.caofeng.pojo.CartItem;
import me.caofeng.pojo.Carts;
import me.caofeng.pojo.Product;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.ICartService;
import me.caofeng.util.JsonUtil;
import netscape.javascript.JSUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.service
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-25 17:01
 * @Version: V1.0
 */
@Service
@Transactional
public class CartService implements ICartService {
    private static Logger LOGGER = LogManager.getLogger(CartService.class);

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Response<CartItem> addItemToCart(Integer productId, Integer count, String userId) {
        //获取商品信息
        Product product = productMapper.getProductInfo(productId);

        //检测商品id是否存在
        if (null == product) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "商品不存在", null);
        }

        //检测购物车中是否存在该商品
        CartItem cartItem = cartMapper.getCartProductId(productId, userId);

        if (null == cartItem) {
            //购物车中没有该商品,新增商品
            cartItem = new CartItem(userId, productId, count, product.getStock());
            int row = cartMapper.addCart(cartItem);
            if (row != 1) {
                return Response.response(ResponseCode.ERROR, "添加至购物车失败", null);
            }
        } else {
            //购物车中存在该商品，更新商品数量
            cartItem.updateQuantity(cartItem.getQuantity() + count, product.getStock());
            int row = cartMapper.updateQuantity(cartItem.getId(), cartItem.getQuantity(),userId);
            if (row != 1) {
                return Response.response(ResponseCode.ERROR, "添加至购物车失败", null);
            }
        }

        cartItem.setProduct(product);

        return Response.response(ResponseCode.SUCCESS, "添加至购物车成功", cartItem);

    }

    @Override
    public Response<Carts> getCarts(String userId) {
        List<CartItem> cartItems = cartMapper.getCarts(userId);
        if (null == cartItems || cartItems.size() == 0 ){
            return Response.response(ResponseCode.EMPTY,"购物车为空",null);
        }
        Carts carts = new Carts();
        for (CartItem cartItem : cartItems) {
            cartItem.setProduct(productMapper.getProductInfo(cartItem.getProductId()));
        }
        carts.setCartItems(cartItems);
        return Response.response(ResponseCode.SUCCESS, "获取购物车成功", carts);
    }

    @Override
    public Response<Carts> updateItemCount(Integer cartItemId, Integer count, String userId) {
        CartItem cartItem = cartMapper.getCartById(cartItemId, userId);

        if (null == cartItem) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "商品不存在", null);
        }

        Product product = productMapper.getProductInfo(cartItem.getProductId());

        if (null == product) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "商品不存在", null);
        }

        if (count > product.getStock()) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "没有货了", null);
        }

        int row = 0;

        if (count <= 0) {
            row = cartMapper.deleteCartItem(cartItemId,userId);
        } else {
            row = cartMapper.updateQuantity(cartItemId, count,userId);
        }

        if (row != 1) {
            return Response.response(ResponseCode.ERROR, "更新商品数量失败", null);
        }

        return this.getCarts(userId);
    }

    @Override
    public Response<Carts> deleteCartItem(Integer cartItemId, String userId) {
        int row = cartMapper.deleteCartItem(cartItemId,userId);
        if (row != 1){
            return Response.response(ResponseCode.ERROR,"删除购物车失败",null);
        }
        return this.getCarts(userId);
    }

    @Override
    public Response<Carts> updateCartItemCheckStatus(Integer cartItemId, CheckStatus status, String userId) {
        int row = cartMapper.updateCartItemStatus(cartItemId,userId,status);
        if ( row != 1){
            return Response.response(ResponseCode.ERROR,"修改购物车状态失败",null);
        }
        return this.getCarts(userId);
    }

    @Override
    public Response<Carts> updateCartsStatus(String userId,CheckStatus status) {
        int row = cartMapper.updateCartsStatus(userId,status);
        if (row < 1){
            return Response.response(ResponseCode.ERROR,"修改购物车状态失败",null);
        }
        return this.getCarts(userId);
    }

    @Override
    public Response clearCart(String userId) {
        int row = cartMapper.clearCart(userId);
        if (row < 1){
            return Response.response(ResponseCode.ERROR,"清空购物车失败",null);
        }
        return Response.response(ResponseCode.SUCCESS,"清空购物车成功",null);
    }


}
